package com.zclcs.utils;

import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zclcs.model.CurrentUser;
import com.zclcs.model.Menu;
import com.zclcs.model.User;
import com.zclcs.service.UserService;

/**
 * 上下文工具类
 * 
 * @author 周成龙
 *
 */
@Component
public final class ServletContextUtil {

	private ServletContextUtil() {

	}

	@Autowired
	private UserService service;

	private static UserService userService;

	@PostConstruct
	public void init() {
		ServletContextUtil.userService = service;
	}

	/**
	 * 返回当前登陆人信息
	 * 
	 * @return
	 */
	public static CurrentUser getCurrentUser() {

		User user = userService.findByUsername(getUsername());
		CurrentUser currentUser = new CurrentUser();
		currentUser.setName(user.getName());
		currentUser.setToken(SecurityUtils.getSubject().getSession().getId());
		currentUser.setUuid(user.getUuid());
		return currentUser;

	}

	/**
	 * 返回当前登陆人信息
	 * 
	 * @return
	 */
	public static CurrentUser getUserInfo() {

		User user = new User();
		user.setUsername(getUsername());
		return userService.getCurrentUser(user);

	}

	/**
	 * 返回当前登陆人权限
	 * 
	 * @return
	 */
	public static List<Menu> getServerRoute() {

		User user = new User();
		user.setUsername(getUsername());
		return userService.selectAccessMenus(user);

	}

	/**
	 * 返回当前登陆人账号
	 * 
	 * @return
	 */
	public static String getUsername() {

		return (String) SecurityUtils.getSubject().getPrincipal();

	}
}
